package com.ruoyi.tester.controller;

import java.util.List;

import com.ruoyi.tester.domain.TesterLoginConfig;
import com.ruoyi.tester.service.ITesterLoginConfigService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.tester.domain.TesterTask;
import com.ruoyi.tester.service.ITesterTaskService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 任务信息Controller
 * 
 * @author SJ
 * @date 2022-04-24
 */
@Controller
@RequestMapping("/tester/task")
public class TesterTaskController extends BaseController
{
    private String prefix = "tester/task";

    @Autowired
    private ITesterTaskService testerTaskService;
    @Autowired
    private ITesterLoginConfigService testerLoginConfigService;

    @RequiresPermissions("tester:task:view")
    @GetMapping()
    public String task()
    {
        return prefix + "/task";
    }

    /**
     * 查询任务信息列表
     */
    @ApiOperation("查询任务信息列表")
    @RequiresPermissions("tester:task:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TesterTask testerTask)
    {
        startPage();
        List<TesterTask> list = testerTaskService.selectTesterTaskList(testerTask);
        return getDataTable(list);
    }

    /**
     * 导出任务信息列表
     */
    @RequiresPermissions("tester:task:export")
    @Log(title = "任务信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TesterTask testerTask)
    {
        List<TesterTask> list = testerTaskService.selectTesterTaskList(testerTask);
        ExcelUtil<TesterTask> util = new ExcelUtil<TesterTask>(TesterTask.class);
        return util.exportExcel(list, "任务信息数据");
    }

    /**
     * 新增任务信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<TesterLoginConfig> loginConfigs = testerLoginConfigService.selectAll();
        mmap.put("loginConfigs", loginConfigs);
        return prefix + "/add";
    }

    /**
     * 新增保存任务信息
     */
    @ApiOperation("新增保存任务信息")
    @RequiresPermissions("tester:task:add")
    @Log(title = "任务信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TesterTask testerTask)
    {
        testerTaskService.insertTesterTask(testerTask);
        return success(testerTask.getTaskId());
    }

    /**
     * 修改任务信息
     */
    @RequiresPermissions("tester:task:edit")
    @GetMapping("/edit/{taskId}")
    public String edit(@PathVariable("taskId") Long taskId, ModelMap mmap)
    {
        TesterTask testerTask = testerTaskService.selectTesterTaskByTaskId(taskId);
        List<TesterLoginConfig> loginConfigs = testerLoginConfigService.selectAll();
        mmap.put("testerTask", testerTask);
        mmap.put("loginConfigs", loginConfigs);
        return prefix + "/edit";
    }

    /**
     * 修改保存任务信息
     */
    @ApiOperation("修改保存任务信息")
    @RequiresPermissions("tester:task:edit")
    @Log(title = "任务信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TesterTask testerTask)
    {
        return toAjax(testerTaskService.updateTesterTask(testerTask));
    }

    /**
     * 删除任务信息
     */
    @ApiOperation("删除任务信息")
    @RequiresPermissions("tester:task:remove")
    @Log(title = "任务信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(testerTaskService.deleteTesterTaskByTaskIds(ids));
    }
}
